/**
 * VIP信息处理工具函数
 */

/**
 * 格式化股东到期时间
 * @param {number} timestamp 时间戳
 * @returns {string} 格式化后的时间字符串
 */
export function formatExpireTime(timestamp) {
	if (!timestamp) {
		return "无到期时间";
	}

	const expireDate = new Date(timestamp);
	return expireDate.toLocaleString("zh-CN", {
		year: "numeric",
		month: "2-digit",
		day: "2-digit",
		hour: "2-digit",
		minute: "2-digit",
	});
}

/**
 * 计算VIP剩余天数
 * @param {number} timestamp 到期时间戳
 * @returns {number} 剩余天数
 */
export function calculateRemainingDays(timestamp) {
	if (!timestamp) {
		return 0;
	}

	const expireDate = new Date(timestamp);
	const now = new Date();

	if (expireDate < now) {
		return 0;
	}

	return Math.ceil((expireDate - now) / (1000 * 60 * 60 * 24));
}

/**
 * 获取VIP状态信息
 * @param {number} timestamp 到期时间戳
 * @returns {object} 状态信息对象
 */
export function getVipStatus(timestamp) {
	if (!timestamp) {
		return {
			text: "无到期时间",
			class: "status-normal",
			isExpired: false,
			isWarning: false,
		};
	}

	const expireDate = new Date(timestamp);
	const now = new Date();

	if (expireDate < now) {
		return {
			text: "已过期",
			class: "status-expired",
			isExpired: true,
			isWarning: false,
		};
	}

	const remainingDays = Math.ceil((expireDate - now) / (1000 * 60 * 60 * 24));

	if (remainingDays <= 30) {
		return {
			text: `有效 (剩余${remainingDays}天)`,
			class: "status-warning",
			isExpired: false,
			isWarning: true,
		};
	}

	return {
		text: `有效 (剩余${remainingDays}天)`,
		class: "status-active",
		isExpired: false,
		isWarning: false,
	};
}

/**
 * 格式化VIP等级名称
 * @param {number} vipLevel VIP等级
 * @param {array} vipList VIP列表
 * @returns {string} VIP等级名称
 */
export function formatVipLevelName(vipLevel, vipList = []) {
	if (!vipLevel || vipLevel === 0) {
		return "普通用户";
	}

	const currentVip = vipList.find((item) => item.lv === vipLevel);
	return currentVip ? currentVip.name : `VIP${vipLevel}`;
}

/**
 * 格式化用户角色
 * @param {array} roles 用户角色数组
 * @returns {string} 格式化后的角色字符串
 */
export function formatUserRoles(roles = []) {
	if (!roles || roles.length === 0) {
		return "普通用户";
	}

	const roleMap = {
		shareholder: "股东",
		admin: "管理员",
		vip: "VIP会员",
		user: "普通用户",
	};

	return roles.map((role) => roleMap[role] || role).join(", ");
}

/**
 * 获取VIP到期日期的简短显示文本
 * @param {number} timestamp 到期时间戳
 * @returns {string} 简短的到期日期文本
 */
export function getExpireDateText(timestamp) {
	if (!timestamp) {
		return "永久有效";
	}

	const expireDate = new Date(timestamp);
	const now = new Date();

	if (expireDate < now) {
		return "已过期";
	}

	const year = expireDate.getFullYear();
	const month = String(expireDate.getMonth() + 1).padStart(2, "0");
	const day = String(expireDate.getDate()).padStart(2, "0");

	return `${year}-${month}-${day} 到期`;
}

/**
 * 分析用户VIP信息并打印到控制台
 * @param {object} userInfo 用户信息对象
 * @param {array} vipList VIP列表
 */
export function analyzeUserVipInfo(userInfo, vipList = []) {
	console.log("=== 用户VIP信息分析 ===");
	console.log("用户ID:", userInfo._id);
	console.log("昵称:", userInfo.nickname);
	console.log("手机号:", userInfo.mobile);
	console.log("VIP等级:", userInfo.vip_lv);
	console.log("VIP等级名称:", formatVipLevelName(userInfo.vip_lv, vipList));
	console.log("股东到期时间戳:", userInfo.shareholder_expire_time);
	console.log(
		"格式化到期时间:",
		formatExpireTime(userInfo.shareholder_expire_time)
	);
	console.log(
		"到期日期文本:",
		getExpireDateText(userInfo.shareholder_expire_time)
	);
	console.log(
		"剩余天数:",
		calculateRemainingDays(userInfo.shareholder_expire_time)
	);

	const status = getVipStatus(userInfo.shareholder_expire_time);
	console.log("状态信息:", status);

	console.log("用户角色:", formatUserRoles(userInfo.role));
	console.log(
		"注册时间:",
		new Date(userInfo.register_date).toLocaleString("zh-CN")
	);
	console.log(
		"最后登录:",
		new Date(userInfo.last_login_date).toLocaleString("zh-CN")
	);
	console.log("========================");
}

/**
 * 获取用户VIP的完整信息对象
 * @param {object} userInfo 用户信息
 * @param {array} vipList VIP列表
 * @returns {object} 完整的VIP信息对象
 */
export function getUserVipInfo(userInfo, vipList = []) {
	const status = getVipStatus(userInfo.shareholder_expire_time);

	return {
		userId: userInfo._id,
		nickname: userInfo.nickname,
		mobile: userInfo.mobile,
		vipLevel: userInfo.vip_lv,
		vipLevelName: formatVipLevelName(userInfo.vip_lv, vipList),
		expireTimestamp: userInfo.shareholder_expire_time,
		expireTimeFormatted: formatExpireTime(userInfo.shareholder_expire_time),
		expireDateText: getExpireDateText(userInfo.shareholder_expire_time),
		remainingDays: calculateRemainingDays(userInfo.shareholder_expire_time),
		status: status,
		roles: formatUserRoles(userInfo.role),
		registerTime: new Date(userInfo.register_date).toLocaleString("zh-CN"),
		lastLoginTime: new Date(userInfo.last_login_date).toLocaleString("zh-CN"),
		updateTime: new Date(userInfo.update_time).toLocaleString("zh-CN"),
	};
}
